package com.boottest.better.test.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.boottest.better.sql.entity.RoleInfo;
import com.boottest.better.sql.entity.User;
import com.boottest.better.sql.entity.UserInfo;
import com.boottest.better.sql.mapper.RoleInfoMapper;
import com.boottest.better.sql.mapper.UserInfoMapper;
import com.boottest.better.sql.mapper.UserMapper;

@RestController
@RequestMapping("/userdb")
public class DBTestController {

    @Autowired
    private UserInfoMapper userMapper;

    @Autowired
    private RoleInfoMapper roleMapper;

    @Autowired
    private UserMapper userbatchmapper;

    @RequestMapping("/findByid")
    @ResponseBody
    public UserInfo findUser(Long id) {
        return userMapper.findById(id);
    }

    @RequestMapping("/queryUser")
    @ResponseBody
    public List<UserInfo> queryUser() {
        return userMapper.queryUsers();
    }

    @RequestMapping("/findRoleByid")
    @ResponseBody
    public RoleInfo findRole(Long id) {
        return roleMapper.findRoleByid(id);
    }

    @RequestMapping("/queryRole")
    @ResponseBody
    public List<RoleInfo> queryRole() {
        return roleMapper.queryRoles();
    }

    @RequestMapping("/insertBatch")
    @ResponseBody
    public List<User> insertBatchUser() {
        List<User> lists = new ArrayList<User>();

        for (int i = 5; i < 10; i++) {
            User u = new User();
            u.setId(Long.parseLong(i + ""));
            u.setAge(i + 1);
            u.setName("xxx" + i);
            lists.add(u);
        }

        userbatchmapper.insertBatchUser(lists);
        return lists;
    }

}
